#include <iostream>

using std::cout;
using std::endl;

//课下思考题：引用与指针的区别？

void test()
{
    //1、引用是变量的别名，操作引用与操作实体的效果是一样的
    //2、引用的地址与第一次绑定的变量的地址保持一致，即使引用
    //又重新与一个新的变量关联
    //3、C++中提出引用的目的就是为了减少指针的使用
    int number = 10;
    int &ref = number;//ref就是number的引用
    cout << "number = " << number << endl;
    cout << "ref = " << ref << endl;
    printf("&number = %p\n", &number);
    printf("&ref = %p\n", &ref);

    cout << endl << "改变number的值" << endl;
    number = 200;
    cout << "number = " << number << endl;
    cout << "ref = " << ref << endl;

    cout << endl << "改变ref的值" << endl;
    ref = 300;
    cout << "number = " << number << endl;
    cout << "ref = " << ref << endl;

    cout << endl << endl;
    int value = 111;
    ref = value;
    cout << "number = " << number << endl;
    cout << "value = " << value << endl;
    cout << "ref = " << ref << endl;
    printf("&value = %p\n", &value);
    printf("&number = %p\n", &number);
    printf("&ref = %p\n", &ref);

    cout << endl << endl;
    //4、引用在定义的时候必须要进行初始化，不能独立存在，
    //引用已经一经绑定，就不能改变其指向,引用的本质
    //就是一个指针,常量指针
    /* int &df;//error */
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

